package com.yang.service.Impl;


import com.yang.config.QiNiuYunConfig;
import com.yang.domain.Result;
import com.yang.enums.AppHttpCodeEnum;
import com.yang.exception.SystemException;
import com.yang.service.UploadService;
import com.yang.utils.AliyunOssUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;

/**
 * <p>
 * 上传文件实现类
 * </p>
 * @author yang
 * @date 2023/5/29
 */
@Service
@Data
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Resource
    private QiNiuYunConfig qiNiuYunConfig;

    @Resource
    private AliyunOssUtil aliyunOssUtil;

    @Override
    public Result uploadImg(MultipartFile file) {

        if (file == null) {
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }

        //1.获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //2.对原始文件名进行安全性判断
        if (originalFilename == null){
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }
        if((!originalFilename.endsWith(".png") && !originalFilename.endsWith(".jpg"))){
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }

        String url = null;
        try {
            if (!"".equals(originalFilename.trim())) {
                File newFile = new File(originalFilename);

                FileOutputStream os = new FileOutputStream(newFile);
                os.write(file.getBytes());
                os.close();

                //把file里的内容复制到奥newFile中
                file.transferTo(newFile);
                String upload = aliyunOssUtil.upload(newFile);
                
                url = "http://yang-blog-master.oss-cn-hangzhou.aliyuncs.com/" + upload;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.okResult(url);
    }

//    @Override
//    public Result uploadImg(MultipartFile img) {
//        //1.获取原始文件名
//        String originalFilename = img.getOriginalFilename();
//
//        //2.对原始文件名进行安全性判断
//        if (originalFilename == null){
//
//            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
//        }
//
//        if((!originalFilename.endsWith(".png") && !originalFilename.endsWith(".jpg"))){
//
//            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
//        }
//
//        //3.使用自定义工具生成文件路径名
//        String filePath = PathUtils.generateFilePath(originalFilename);
//
//        //4.按照指定的文件规则名进行上传
//        String url = uploadOss(img,filePath);
//
//        return Result.okResult(url);
//    }
//
//    private String uploadOss(MultipartFile imgFile, String filePath){
//        //构造一个带指定 Region 对象的配置类
//        Configuration cfg = new Configuration(Region.autoRegion());
//        //...其他参数参考类注释
//        UploadManager uploadManager = new UploadManager(cfg);
//        //默认不指定key的情况下，以文件内容的hash值作为文件名
//
//        try {
//            InputStream inputStream = imgFile.getInputStream();
//            Auth auth = Auth.create(qiNiuYunConfig.getAccessKey(), qiNiuYunConfig.getSecretKey());
//            String upToken = auth.uploadToken(qiNiuYunConfig.getBucketName());
//            try {
//                Response response = uploadManager.put(inputStream, filePath,upToken,null, null);
//                //解析上传成功的结果
//                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//
//                log.info("putRet.key:{}",putRet.key);
//
//                log.info("putRet.hash:{}",putRet.hash);
//
//                //拿到文件上传成功的链接
//                return qiNiuYunConfig.getUrl() + filePath;
//
//            } catch (QiniuException ex) {
//                log.debug("QiniuException,ex.toString:{},ex.bodyString{}",ex.response.toString(),ex.response.bodyString());
//            }
//        } catch (Exception ex) {
//            log.debug("QiniuException：{}",ex.toString());
//        }
//
//        return qiNiuYunConfig.getUrl() + filePath;
//
//    }
}
